<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/conn.asp"-->
<!--#include file="lib/saferequest.asp"-->
<!--#include file="lib/error_write.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<link rel="stylesheet" href="lib/sheet.css" type="text/css" />
<link type="text/css" rel="stylesheet" href="lib/ui/themes/base/ui.all.css" />
<!-- jquery和ui -->
<script type="text/javascript" src="lib/jquery.js"></script>
<script type="text/javascript" src="lib/ui/ui.core.js"></script>
<script type="text/javascript" src="lib/ui/ui.datepicker.js"></script>
<script type="text/javascript" src="lib/ui/i18n/ui.datepicker-zh-CN.js"></script>
<script type="text/javascript">
	$(function() {
		$.datepicker.setDefaults({
			changeMonth: true,
			changeYear: true
		});
		$(".datepicker").datepicker();
		$(".contable").addClass("ct");	//载入table样式
		$(".contable th").addClass("ch");	//载入th样式
		$(".contable td").addClass("ld");	//载入td样式
		$("#dd,#xx").change(function(){
			$.get("yj.asp",{didian:$("#dd").html(),xiangxing:$("#xx").html()},function(data){
				//alert(data);
				if (!data){
					$("#yj,#rlf,#sjrlf,#glf,#ltf,#chanzhi,#jsj").val(0);
					return false;
				}
				//$("#yj").val(data.yunjia);
				$("#zdyj").html(data.yunjia);
				$("#rlf").val(data.zdrlf);
				$("#glf").val(data.zdglf);
				$("#chanzhi").val(data.chanzhi);
				if(data.jsj==""){$("#jsj").val("0")} else {$("#jsj").val(data.jsj)};
				//$("#dz").val((parseFloat($("#jgf").val())+parseFloat($("#jsj").val()))*0.6);
				$("#zddz").html((parseFloat($("#jgf").val())+parseFloat($("#jsj").val()))*0.6);
			},"json");
		});

		$("#tixiang,#huanxiang").change(function(){
			var tx=hx=0;
			$.get("tx.asp",{tx:$("#tixiang").val()},function(data){
				//alert(data);
				(!data)? tx=0 : tx=data.tx;
				$("#jgf").val(tx+hx);
				//$("#dz").val((tx+hx+parseFloat($("#jsj").val()))*0.6);
				$("#zddz").html((tx+hx+parseFloat($("#jsj").val()))*0.6);
			},"json");
			$.get("hx.asp",{hx:$("#huanxiang").val()},function(data){
				//alert(data);
				(!data)? hx=0 : hx=data.hx;
				$("#jgf").val(tx+hx);
				//$("#dz").val((tx+hx+parseFloat($("#jsj").val()))*0.6);
				$("#zddz").html((tx+hx+parseFloat($("#jsj").val()))*0.6);
			},"json");
		});
		$("#xh").blur(function(){
			$("#xh_memo").load("container_coding.asp?xh="+$(this).val());
		});
		$("#thf").keypress(function(){
			if($("#tixiang").val()==""&&$("#huanxiang").val()=="") alert("请输入提箱或还箱!");
		});
		$("#jgf").blur(function(){
			if($("#thf").val()!=""&&$("#jgf").val()=="") alert("请输入进港费!");
			if($("#jsj").val()=="") jsj=0; else jsj=Number($("#jsj").val());
			if($("#jgf").val()=="") jgf=0; else jgf=Number($("#jgf").val());
			//$("#dz").val((parseFloat($("#jgf").val())+parseFloat($("#jsj").val()))*0.6);
			$("#zddz").html((parseFloat($("#jgf").val())+parseFloat($("#jsj").val()))*0.6);
			$("#glcbf").html(((jsj+jgf)*0.1).toFixed(2));
		});
		$("#jsj").blur(function(){
			
			if($("#jsj").val()=="") jsj=0; else jsj=Number($("#jsj").val());
			if($("#jgf").val()=="") jgf=0; else jgf=Number($("#jgf").val());
			
			$("#glcbf").html(((jsj+jgf)*0.1).toFixed(2));
		});
		$("#djf").blur(function(){
			if($("#xx").val()!="20"&&Number($("#djf").val())<70) alert("吊机费小于70，箱型应该是20，请修改!");
			$("#xx").focus();
		});
		$("#tyr").keyup(function(){
			var tyr=$("#tyr");
			$("#ttyr").css("left",(tyr.offset().left).toString()+"px").css("top",(tyr.offset().top+tyr.height()+5).toString()+"px").css("width",(tyr.width()+6).toString()+"px");
			//alert(tyr.offset().left);
			//e.preventDefault();
			if(tyr.val().length>0){
				$.get("tyr.asp",{tyr:$(this).val()},function(data){
					if(!data.length){
						$("#ttyr").css("visibility","hidden");
					}else{
						//alert(data[0].tyr);

						var ttyr="";
						for(var i=0;i<data.length;i++)
						{
							ttyr+="<tr><td class='gac_c'>"+data[i].tyr+"</td></tr>";
							//alert(ttyr);
						}
						//alert(ttyr);
						$("#ttyr").html(ttyr).css("visibility","visible");
						$("#ttyr tr").mouseover(function(){	//对返回的dom进行mouseowver事件捕获
							$(this).toggleClass("gac_b");	//添加css样式
						}).mouseout(function(){	//对dom进行mouseout事件捕获
							$(this).toggleClass("gac_b");	//移除css样式
						});
						$("#ttyr td").click(function(){
							tyr.val($(this).text());
							$("#ttyr").css("visibility","hidden");
						});
						tyr.blur(function(){
							setTimeout("$('#ttyr').css('visibility','hidden');", 200);
						});
					}
				},"json");
			}
		});
	});

//去除字符串的空格
function gf_trim(as_string)
{
   while(as_string.length > 0 && as_string.indexOf(" ")==0) as_string = as_string.substr(1);
   while(as_string.length > 0 && as_string.lastIndexOf(" ")==(as_string.length-1)) as_string = as_string.substr(0,as_string.length-1);
   return as_string;
}
//集装箱箱号验证
//功能：验证集装箱箱号：
//参数：
//   as_cntrno 是否符合国际标准，
//返回值：True 符合国际标准或强行通过(特殊箱号)
//举例：gf_chkcntrno( 'TEXU2982987', 0 )
function chkcntrno(as_cntrno,ai_choice)
{
 var fi_ki;
 var fi_numsum;
 var fi_nummod;
 var fai_num = new Array(11);
 var fb_errcntrno=false;

 if (as_cntrno==null) return true; //null不进行验证
 if (gf_trim(as_cntrno)=="") return true; //空不进行验证

 if (as_cntrno.length == 11)   //国际标准为11位，最后一位是校验位，若不是11位肯定不是标准箱
 { for(fi_ki=1;fi_ki<=11;fi_ki++)
   fai_num[fi_ki] = 0;
  for(fi_ki=1;fi_ki<=4;fi_ki++)     //根据国际标准验证法则处理箱号前面的4个英文字母
  {
   fch_char=as_cntrno.charAt(fi_ki-1).toUpperCase();
   switch(true)
   { case (fch_char=="A"):{fai_num[fi_ki] = 10;break;}
    case (fch_char>="V" && fch_char<="Z"):{fai_num[fi_ki] = fch_char.charCodeAt() - 52;break;}
    case (fch_char>="L" && fch_char<="U"):{fai_num[fi_ki] = fch_char.charCodeAt() - 53;break;}
    default:{fai_num[fi_ki] = fch_char.charCodeAt() - 54;break;}
   }
  }
  for(fi_ki=5;fi_ki<=11;fi_ki++)
  {  fch_char=as_cntrno.charAt(fi_ki-1);
   fai_num[fi_ki] = parseInt(fch_char); //ctype((mid(as_cntrno, fi_ki, 1)), integer)
      }
  fi_numsum = 0

  for(fi_ki=1;fi_ki<=10;fi_ki++)
  {
   fi_sqr = 1;
   for(i=1;i<fi_ki;i++){fi_sqr *=2;}
   fi_numsum += fai_num[fi_ki] * fi_sqr;
  }

  if (as_cntrno.substr(0,4) == "HLCU") fi_numsum = fi_numsum - 2; //hapaq lloyd的柜号与国际标准相差2
  fi_nummod = fi_numsum % 11;
  if (fi_nummod == 10) fi_nummod = 0;
  if (fi_nummod == fai_num[11]){
	fb_errcntrno = true;
  }else{
	alert("箱号校验错误，请修正！");
	document.getElementById("xh").focus();
 }
  return fb_errcntrno;
 }else{
	alert("箱号不是11位，请检查！");
	document.getElementById("xh").focus();
    return fb_errcntrno;
 }
}
</script>
<!--#include file="lib/keydown.asp"-->

</head>
<body>
<%
If Not Session("MM_Reg") Then
	response.write("您没有登陆，请登陆后使用!<br>")
	response.write "<a href='javascript:history.go(-1)'> << 返回上一页</a><br> "
	response.end
End If
if Session("MM_Flag")<4 then
	response.write("您没有编辑权限！<br>")
	response.write "<a href='javascript:history.go(-1)'> << 返回上一页</a><br> "
	response.End
end if


if request.QueryString("id")<>"" then id=replace(trim(cstr(request.QueryString("id"))),"'","''")

if request.form("tj")="退单" or request.form("tj")="保存" Then
	if request.form("tj")="退单" Then post=1
	if request.form("tj")="保存" Then post=2
	tyr=trim(replace(request.form("tyr"),"'","''"))
	if tyr="" then error_write "2","没有填写[托运人]"
	cyr=trim(replace(request.form("cyr"),"'","''"))
	if cyr="" then error_write "3","没有填写[承运人]"
	xh=trim(replace(request.form("xh"),"'","''"))
	fh=trim(replace(request.form("fh"),"'","''"))
	tx=trim(replace(request.form("tixiang"),"'","''"))
	hx=trim(replace(request.form("huanxiang"),"'","''"))
	cmhc=trim(replace(request.form("cmhc"),"'","''"))
	tdh=trim(replace(request.form("tdh"),"'","''"))
	djf=trim(replace(request.form("djf"),"'","''"))
	if djf="" then djf=0
	glf=trim(replace(request.form("glf"),"'","''"))
	if glf="" then glf=0
	ltf=trim(replace(request.form("ltf"),"'","''"))
	if ltf="" then ltf=0
	rlf=trim(replace(request.form("rlf"),"'","''"))
	if rlf="" then rlf=0
	sjrlf=trim(replace(request.form("sjrlf"),"'","''"))
	if sjrlf="" then sjrlf=0
	xlf=trim(replace(request.form("xlf"),"'","''"))
	if xlf="" then xlf=0
	thf=trim(replace(request.form("thf"),"'","''"))
	if thf="" then thf=0
	If (tx="" And hx="") And thf<>0 Then error_write "4","请填写[提箱]或[还箱]"
	dcf=trim(replace(request.form("dcf"),"'","''"))
	if dcf="" then dcf=0
	qtf=trim(replace(request.form("qtf"),"'","''"))
	if qtf="" then qtf=0
	cbbz=trim(replace(request.form("cbbz"),"'","''"))
	yj=trim(replace(request.form("yj"),"'","''"))
	if yj="" then yj=0
	chanzhi=trim(replace(request.form("chanzhi"),"'","''"))
	if chanzhi="" then chanzhi=0
	jsj=trim(replace(request.form("jsj"),"'","''"))
	if jsj="" then jsj=0
	dz=trim(replace(request.form("dz"),"'","''"))
	if dz="" then dz=0
	jgf=trim(replace(request.form("jgf"),"'","''"))
	if jgf="" then jgf=0
	If thf<>0 And jgf=0 Then error_write "5","请填写[进港费]"
	bz=trim(replace(request.form("bz"),"'","''"))
	js=request.form("js")
	yf=request.form("yf")
	baozhang=saferequest("baozhang",0)
	baozhangchk=saferequest("baozhangchk",0)
	baozhangsjchk=saferequest("baozhangsjchk",0)
	If baozhang="" Then
		baozhang=False
		baozhangsj="1900-1-1"
	ElseIf baozhangchk="1" then
		baozhang=True
		baozhangsj=baozhangsjchk
	Else
		baozhang=True
		baozhangsj=date()
	End If
	if js="" then js=false else js=true
	if yf="" then yf=false else yf=true
	if xh<>"" or fh<>"" then
		sql="select * from yb where xh='"&xh&"' and fh='"&fh&"' and id<>"&id
		set chongfu=server.createobject("adodb.recordset")
		chongfu.open sql,conn,1,1
		if not chongfu.eof then	error_write "24","箱号和封号重复，请检查！"
	end if
	sql="update yb set tyr='"&tyr&"',cyr="&cyr&",xh='"&xh&"',cmhc='"&cmhc&"',tdh='"&tdh&"',fh='"&fh&"',tx='"&tx&"',hx='"&hx&"',djf="&djf&",glf="&glf&",ltf="&ltf&",rlf="&rlf&",sjrlf="&sjrlf&",xlf="&xlf&",thf="&thf&",dcf="&dcf&",qtf="&qtf&",cbbz='"&cbbz&"',yj="&yj&",chanzhi="&chanzhi&",jsj="&jsj&",dz="&dz&",jgf="&jgf&",bz='"&bz&"',js="&js&",yf="&yf&",baozhang="&baozhang&",gxry='"&session("MM_Name")&"',gxsj='"&Now()&"',baozhangsj='"&baozhangsj&"',post="&post&" where id="&id
	'response.write sql
	set cmd=Server.CreateObject("ADODB.Command")
	cmd.ActiveConnection=conn
	cmd.CommandText = sql
	cmd.Execute
	If post=1 Then
		response.write("退单成功！")
		response.End
	End If
	response.write("编辑成功，可以继续编辑本记录")
	view_url="view_cd.asp?id="&id
	response.Redirect(view_url)
end if

set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from yb LEFT JOIN cl ON yb.cyr = cl.clid where id="&id
rs.open sql,conn,1,1

%>
<form name="form1" method="post" action="#">
<table class="contable">
	<thead>
		<tr>
			<th colspan="4" class="lh"><font size="4">基本信息</font>(录入时间:<%=rs("lrsj")%>&nbsp;下单人:<%=rs("xdr")%>)</th>
    </tr>
	</thead>
  <tr>
    <th>序号</th>
    <td><%=id%></td>
    <th>货单日期</th>
    <td><%=rs("rq")%></td>
  </tr>
  <tr>
    <th>详细厂名</th>
    <td colspan="4"><%=rs("client")%></td>
  </tr>
	<tr>
		<th>联系方式</th>
    <td colspan="4"><%=rs("contacts")%></td>
  </tr>
	<tr>
		<th>货主</th>
    <td colspan="4"><%=rs("hz")%></td>
  </tr>
  <tr>
    <th>托运人</th>
    <td>
		<select id="tyr" name="tyr">
			<option value=""></option>
		<%
		set rs_tyr = Server.CreateObject("ADODB.Recordset")
sql="select tyr from (select tyr,count(tyr) as n from yb group by tyr) order by n desc"
rs_tyr.open sql,conn,1,1
While Not rs_tyr.eof
%>
		<option value="<%=rs_tyr("tyr")%>" <%if rs_tyr("tyr")=rs("tyr") then response.write("selected")%>><%=rs_tyr("tyr")%></option>
<%
rs_tyr.movenext
Wend
rs_tyr.close
%>					
		</select></td>
    <th>承运人</th>
    <td>
	<select name="cyr">
		<option value="0"></option>
	<%
		set rs_cyr = Server.CreateObject("ADODB.Recordset")
		sql="select clid,clm,cph from (select cl.clid as a,count(cl.clid) as n from(SELECT cl.clid , yb.cyr FROM cl LEFT JOIN yb ON cl.clid = yb.cyr) group by cl.clid order by count(cl.clid) desc),cl where cl.clid=a"
		rs_cyr.open sql,conn,1,1
		while not rs_cyr.eof
	%>
		<option value="<%=rs_cyr("clid")%>" <%if rs_cyr("clid")=rs("cyr") then response.write("selected")%>><%=rs_cyr("clm")%>(<%=rs_cyr("cph")%>)</option>
	<%
			rs_cyr.movenext()
		wend
		rs_cyr.close

	%>
	</select>
	</td>
  </tr>
  <tr>
    <th>地点</th>
    <td><span id="dd"><%=rs("dd")%></span></td>
		<th>时间</th>
		<td><%If IsDate(rs("sj")) Then response.write(formatdatetime(rs("sj"),4))%></td>
	</tr>
	<tr>
    <th>船名/航次</th>
    <td><%if rs("cmhc")="" then response.write("<input name='cmhc' id='cmhc' />") else response.write("<input name='cmhc' id='cmhc' value='"&rs("cmhc")&"' readonly='readonly' />")%></td>
		<th>下单日期</th>
		<td><%=rs("xdrq")%></td>
  </tr>
  <tr>
    <th>提单号</th>
    <td><%if rs("tdh")="" then response.write("<input name='tdh' id='tdh' />") else response.write("<input name='tdh' id='tdh' value='"&rs("tdh")&"' readonly='readonly' />")%></td>
    <th>箱型</th>
    <td><span id="xx"><%=rs("xx")%></span></td>
  </tr>
  <tr>
    <th>箱号</th>
    <td><input name="xh" id="xh" value="<%=rs("xh")%>" onblur="chkcntrno(xh.value,0)" /></td>
    <th>封号</th>
    <td><input name="fh" id="fh" value="<%=rs("fh")%>" /></td>
  </tr>
	<thead>
		<tr>
			<th colspan="4" class="lh"><font size="4">成本费用</font></th>
    </tr>
	</thead>
  <tr>
    <th>提箱</th>
    <td>
		<select name="tixiang" id="tixiang">
        <option value=""></option>
		<%
		set rs_tx = Server.CreateObject("ADODB.Recordset")
		sql="select * from tx"
		rs_tx.open sql,conn,1,1
		while not rs_tx.eof
		%>
        <option value="<%=rs_tx("tixiang")%>" <%If rs("tx")=rs_tx("tixiang") Then response.write("selected='selected'")%>><%response.write(rs_tx("tixiang")&"("&rs_tx("tixiangjiage")&")")%></option>
	<%
			rs_tx.movenext()
		wend
		rs_tx.close
	%>
      </select>
</td>
    <th>还箱</th>
    <td>
	<select name="huanxiang" id="huanxiang">
        <option value=""></option>
		<%
		set rs_hx = Server.CreateObject("ADODB.Recordset")
		sql="select * from hx"
		rs_hx.open sql,conn,1,1
		while not rs_hx.eof
		%>
        <option value="<%=rs_hx("huanxiang")%>" <%If rs("hx")=rs_hx("huanxiang") Then response.write("selected='selected'")%>><%response.write(rs_hx("huanxiang")&"("&rs_hx("huanxiangjiage")&")")%></option>
	<%
			rs_hx.movenext()
		wend
		rs_hx.close
	%>
      </select>
</td>
  </tr>
  
  <tr style="display:none">
    <th>吊机费</th>
    <td><input name="djf" id="djf" value="<%=rs("djf")%>" /></td>
    <th>产值</th>
    <td><input name="chanzhi" id="chanzhi" value="<%=rs("chanzhi")%>" /></td>
  </tr>
  <tr style="display:none">
    <th>指导过路费</th>
    <td><input name="glf" id="glf" value="<%=rs("glf")%>" readonly="readonly" /></td>
    <th>实际过路费</th>
    <td><input name="ltf" id="ltf" value="<%=rs("ltf")%>" /></td>
  </tr>
  <tr style="display:none">
    <th>指导燃料费</th>
    <td><input name="rlf" id="rlf" value="<%=rs("rlf")%>" readonly="readonly" /></td>
    <th>实际燃料费</th>
    <td><input name="sjrlf" id="sjrlf" value="<%=rs("sjrlf")%>" /></td>
  </tr style="display:none">
  <tr style="display:none">
    <th>修理费</th>
    <td><input name="xlf" id="xlf" value="<%=rs("xlf")%>" /></td>
    <th>提还费</th>
    <td><input name="thf" id="thf" value="<%=rs("thf")%>" onkeypress="checkthf()" /></td>
  </tr>
  <tr>
    <th>堆存费</th>
    <td><input name="dcf" id="dcf" value="<%=rs("dcf")%>" /></td>
    <th>其他费</th>
    <td><input name="qtf" id="qtf" value="<%=rs("qtf")%>" /></td>
  </tr>
  <tr>
    <th>成本备注</th>
    <td colspan="3"><input name="cbbz" id="cbbz" value="<%=rs("cbbz")%>" size="80" /></td>
  </tr>
	<thead style="display:none">
		<tr>
			<th colspan="4" class="lh"><font size="4">基本费用</font></th>
    </tr>
	</thead>
	<tbody style="display:none">
  <tr>
    <th>应收运费</th>
    <td><input name="yj" id="yj" value="<%=rs("yj")%>" readonly="readonly" title=" 运价=地点&箱型" /> 运价:<span id="zdyj"><%=rs("yj")%></span></td>
    <th>应付运费</th>
    <td><input name="jsj" id="jsj" value="<%=rs("jsj")%>" readonly="readonly" /></td>
  </tr>
  <tr>
    <th>进港费</th>
    <td><input name="jgf" id="jgf" value="<%=rs("jgf")%>" title="进港费=提箱+还箱" /> </td>
    <th>报账</th>
    <td><input name="baozhang" type="checkbox" id="baozhang" value="true" <%if rs("baozhang") then response.write("checked")%> /><input type="hidden" name="baozhangchk" id="baozhangchk" value="<%If rs("baozhang") Then response.write("1")%>" /><input type="hidden" name="baozhangsjchk" id="baozhangsjchk" value="<%=rs("baozhangsj")%>" />&nbsp;</td>
  </tr>
  <tr>
    <th>垫资</th>
    <td><input name="dz" id="dz" value="<%=rs("dz")%>" title="垫资=（应付运费+进港费）×60%" /> 垫资:<span id="zddz"><%=rs("dz")%></span></td>
    <th>管理成本</th>
    <td><span id="glcbf"><%
														if rs("jsj")<>"" or rs("jsj")<>0 or rs("jgf")<>"" or rs("jgf")<>0 then 
															if rs("jsj")="" or isnull(rs("jsj")) then jsj0=0 else jsj0=rs("jsj")
															if rs("jgf")="" or isnull(rs("jgf")) then jgf0=0 else jgf0=rs("jgf")
															response.write(formatnumber((jsj0+jgf0)*0.1,2,-1))
														end if
												%></span></td>
  </tr>
  <tr>
    <th>已收</th>
    <td><input name="js" type="checkbox" id="js" value="true" <%if rs("js") then response.write("checked")%> />&nbsp;</td>
    <th>已付</th>
    <td><input name="yf" type="checkbox" id="yf" value="true" <%if rs("yf") then response.write("checked")%> />&nbsp;</td>
  </tr>
	</tbody>
  <tr>
    <th>注意事项</th>
    <td colspan="3"><%=rs("attentions")%></td>
  </tr>
  <tr>
    <th>车队备注</th>
    <td colspan="3"><input name="bz" id="bz" size="80" value="<%=rs("bz")%>" /></td>
  </tr>
</table>
<br />
<input class="bt" type="submit" name="tj" value="退单" />&nbsp;&nbsp;&nbsp;&nbsp;
<input class="bt" type="submit" name="tj" value="保存" onClick="return confirm('确定编辑货单吗？')" />&nbsp;&nbsp;&nbsp;&nbsp;
<input class="bt" type="reset" />
</form>
<br />
<p>最后更改人:<%=rs("gxry")%>&nbsp;最后更新时间:<%=rs("gxsj")%></p>
<br />
<p>
操作>> <%If (rs("post")=2 And Session("MM_Post")=2) or Session("MM_Post")=0 Then%>| <a href="view_cd.asp?id=<%=id%>">货单查看</a> |<%If Session("MM_Post")=0 Then%> <a href="delete_cd.asp?id=<%=id%>" onClick="return confirm('你真的想删除这个货单吗？')">货单删除</a> |<%End If%><%End If%>
</p>

<%
rs.close
Set rs=Nothing
conn.close
Set conn=nothing
%>
</body>
</html>
